package com.xdu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdu.common.R;
import com.xdu.domain.Book;
import com.xdu.service.BooksService;
import com.xdu.vo.BookVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * 图书管理
 */
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BooksService booksService;

    /**
     * 图书信息分页查询
     *
     * @param page
     * @param pageSize
     * @param bookVo
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, BookVo bookVo) {
        log.info("获取到bookVo参数:"+bookVo);
        //分页构造器
        Page<Book> pageInfo = new Page<>(page, pageSize);

        //条件构造器
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(bookVo.getTitle()), Book::getTitle, bookVo.getTitle());
        queryWrapper.like(Strings.isNotEmpty(bookVo.getAuthor()), Book::getAuthor, bookVo.getAuthor());
        queryWrapper.gt(bookVo.isCanBorrow(), Book::getAvailableCopies, 0);
        queryWrapper.orderByAsc(Book::getId);

        //执行分页查询
        booksService.page(pageInfo, queryWrapper);

        //返回结果
        return R.success(pageInfo);
    }

}
